package com.hd.pzhwlxt.controller;

import cn.hutool.core.util.StrUtil;
import com.hd.pzhwlxt.pojo.StaffBasicInfo;
import com.hd.pzhwlxt.service.impl.StaffBasicInfoServiceImpl;
import com.hd.pzhwlxt.utils.Response;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;

@RestController
public class Login {

    @Autowired
    private StaffBasicInfoServiceImpl staffBasicInfoService;

    @PostMapping("login")
    public Response login(@RequestBody StaffBasicInfo staffBasicInfo, HttpSession session){
        if(StrUtil.isBlank(staffBasicInfo.getStaffName())||StrUtil.isBlank(staffBasicInfo.getPassword())){
            return Response.createErrorResponse("失败了");
        }
        StaffBasicInfo staffBasicInfo1 = staffBasicInfoService.login(staffBasicInfo);
        if(staffBasicInfo1!=null){
            session.setAttribute("admin",staffBasicInfo1);
            return Response.createSuccessResponse("登陆成功",staffBasicInfo1);
        }
        return Response.createErrorResponse("登陆失败");
    }


    @GetMapping("logout")
    public void logout(HttpSession session){
        session.removeAttribute("admin");
    }
}
